package com.tools
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * 资源动态加载器
	 * @author Boder 2013-5-3 下午9:42:52
	 *
	 */
	public class ResLoader
	{
		private var symbolLoader  :  Loader       =  null;
		private var symbolRequest  :  URLRequest  =  null;

		private var appDomain  :  ApplicationDomain;

		private var isLoaded  :  Boolean          =  false;

		public function ResLoader( url  :  String )
		{
			loadTeamSymbol( url );
		}

		private function loadTeamSymbol( url  :  String )  :  void
		{
			isLoaded  =  false;
			symbolLoader  =  new Loader();
			symbolLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, symbolLoader_CompleteHandler );
			symbolRequest  =  new URLRequest( url );
			var loaderContext  :  LoaderContext  =  new LoaderContext( true );
			loaderContext.applicationDomain  =  new ApplicationDomain( ApplicationDomain.currentDomain );
			symbolLoader.load( symbolRequest, loaderContext );
		}

		private function symbolLoader_CompleteHandler( event  :  Event )  :  void
		{
			isLoaded  =  true;
			appDomain  =  symbolLoader.contentLoaderInfo.applicationDomain;
		}

		/**
		 * 根据资源名称取出对应资源
		 * @param fullResName
		 * @return
		 *
		 */
		[Bindable( "change" )]
		public function getResByName( fullResName  :  String )  :  Object
		{
			if ( !isLoaded )
			{
				return null;
			}
			return appDomain.getDefinition( fullResName );
		}
	}
}
